package com.anan.Interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description: 过滤器
 * @author anan
 * @date 2023/03/17/16:31
 **/
@Component
@Slf4j
public class ProjectInterceptor implements HandlerInterceptor {


    /**
     *  在原始操作之前
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String contentType = request.getHeader("Content-Type");
        System.out.println("content-type:"+contentType);
        String user = request.getParameter("username");
        request.setAttribute("current_user", user);
        response.setHeader("COOKER","test");
        System.out.println(response.getHeader("COOKER") + request.getRequestURI());
        log.info("content-type:"+contentType+ "===" + user);
        return true;//这里代表是否执行原始操作，若改为false则后续操作将不会执行
    }


    /**
     *  在原始访问操作之后
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle...");
        log.info("===== postHandle... =====");
    }


    /**
     *  在拦截操作之后
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion...");
    }
}
